package org.yihang.common.hibernate;

import org.hibernate.boot.model.naming.EntityNaming;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.ImplicitBasicColumnNameSource;
import org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl;

@SuppressWarnings("serial")
public class CustomImplicitNamingStrategy extends ImplicitNamingStrategyLegacyJpaImpl {

	private String currentTableName;

	@Override
	protected String transformEntityName(EntityNaming entityNaming) {
		this.currentTableName = "Prefix_" + super.transformEntityName(entityNaming);
		return "Prefix_" + super.transformEntityName(entityNaming);
	}

	@Override
	public Identifier determineBasicColumnName(ImplicitBasicColumnNameSource source) {
		if (currentTableName != null)
			return toIdentifier(currentTableName + "_" + transformAttributePath(source.getAttributePath()),
					source.getBuildingContext());
		else {
			return super.determineBasicColumnName(source);
		}
	}

}
